import Taro, { Component } from '@tarojs/taro'
import { AtButton } from 'taro-ui'
import classnames from 'classnames'
import PropsType from 'prop-types'
import { View, Image } from '@tarojs/components'
import '../styles/itemerror.scss'

const itemLoading = require('../assete/loading.gif')
const itemBgNone = require('../assete/none_icon.png')
const itemBgSearch = require('../assete/serach_icon.png')
const itemBgList = require('../assete/list_icon.png')
const itemBgFail = require('../assete/fail_icon.png')
const itemBgCar = require('../assete/car_icon.png')
const itemBgInfo = require('../assete/info_icon.png')

export default class DItemError extends Component {

    constructor(props) {
        super(props)
        this.state = {
            bgIcon: itemBgNone
        }
    }

    componentWillMount() {
        this.getImages()
    }

    getImages() {
        let { type = 1 } = this.props
        // const type = 0
        let _obj = {
            0: itemLoading,
            1: itemBgNone,
            2: itemBgSearch,
            3: itemBgList,
            4: itemBgFail,
            5: itemBgCar,
            6: itemBgInfo,
            7: itemBgInfo,
            8: itemBgList,
            9: itemBgList
        }
        this.setState({
            bgIcon: _obj[type]
        })
    }

    getStatusText(type) {
        return {
            $1: '暂无数据',
            $2: '暂无搜索结果，试试其他搜索条件',
            $3: '暂无车源',
            $4: '加载失败，请检查网络',
            $5: '找不到数据',
            $6: '暂时没有消息哦~',
            $7: '功能开发中，请尽情期待！',
            $8: '您还未完成运输车辆登记哦~请先完成登记认证~',
            $9: '暂无该范例图片'
        }[`$${type}`]
    }

    onNewClick() {
        this.props.onNewClick && this.props.onNewClick()
    }

    render() {
        const {
            className,
            type
        } = this.props
        const {
            bgIcon
        } = this.state
        return (
            <View className={classnames('b_itemerror', className)}>
                <View className='b_itemerror__tiptext'>
                    <Image src={bgIcon} className='b_itemerror__images'></Image>
                    <View className='b_itemerror__text'>{this.getStatusText(type)}</View>
                    {
                        type === 4 && <AtButton className='b_itemerror__btn' onClick={this.onNewClick.bind(this)}>刷新</AtButton>
                    }
                </View>
            </View>
        )
    }
}


DItemError.defaultProps = {
    type: 1
}

DItemError.PropsType = {
    className: PropsType.oneOfType[PropsType.string, PropsType.object],
    type: PropsType.number,
    onNewClick: PropsType.func
}